window.db = {
	name: 'rainchat',
	path: '_doc/rainchat.db',
	// 打开数据库
	open: () => {
		if (!db.isOpen()) {
			plus.sqlite.openDatabase({
				name: db.name,
				path: db.path,
				success: function(e) {
					console.log('log info ' + app.formatDate(new Date()) + ': openDatabase success ======> ' + JSON.stringify(e));
					db.createMessageTable();
				},
				fail: function(e) {
					console.log('log error ' + app.formatDate(new Date()) + ': openDatabase failed ======> ' + JSON.stringify(e));
				}
			});
		}
	},
	// 关闭数据库
	close: () => {
		if (db.isOpen()) {
			plus.sqlite.closeDatabase({
				name: db.name,
				path: db.path,
				success: function(e) {
					console.log('log info ' + app.formatDate(new Date()) + ': closeDatabase success ======> ' + JSON.stringify(e));
				},
				fail: function(e) {
					console.log('log error ' + app.formatDate(new Date()) + ': closeDatabase failed ======> ' + JSON.stringify(e));
				}
			});
		}
	},
	isOpen: (name = db.name, path = db.path) => {
		return plus.sqlite.isOpenDatabase({
			name,
			path
		});
	},
	dropTable: (tableName) => {
		db.open();
		let sql = 'drop table ' + tableName;
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('dropTable ' + tableName + ' success!');
			},
			fail: function(e) {
				console.log('dropTable ' + tableName + ' failed: ' + JSON.stringify(e));
			}
		});
	},
	createMessageTable: () => {
		db.open();
		let sql = 'create table if not exists t_chat_message' +
			'(' +
			'    id INTEGER primary key AUTOINCREMENT not null,' +
			'    message_id INTEGER,' +
			'    create_time NUMERIC,' +
			'    message TEXT,' +
			'    status INTEGER,' +
			'    receive_user_id INTEGER,' +
			'    send_user_id INTEGER,' +
			'    type INTEGER,' +
			'    is_myself INTEGER,' +
			'    user_id INTEGER,' +
			'    friend_id INTEGER' +
			')';
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('createMessageTable success!');
			},
			fail: function(e) {
				console.log('createMessageTable failed: ' + JSON.stringify(e));
			}
		});
	},
	createSnapshotMessageTable: () => {
		db.open();
		let sql = 'create table if not exists t_chat_snapshot_message' +
			'(' +
			'    id INTEGER primary key AUTOINCREMENT not null,' +
			'    message_id INTEGER,' +
			'    create_time NUMERIC,' +
			'    message TEXT,' +
			'    status INTEGER,' +
			'    receive_user_id INTEGER,' +
			'    send_user_id INTEGER,' +
			'    type INTEGER,' +
			'    is_myself INTEGER,' +
			'    unread INTEGER,' +
			'    user_id INTEGER,' +
			'    friend_id INTEGER' +
			')';
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('createMessageTable success!');
			},
			fail: function(e) {
				console.log('createMessageTable failed: ' + JSON.stringify(e));
			}
		});
	},
	createDictionariesTable: () => {
		db.open();
		let sql = 'create table if not exists t_dictionaries' +
			'(' +
			'    id INTEGER primary key AUTOINCREMENT not null,' +
			'    create_time NUMERIC,' +
			'    dict_code TEXT,' +
			'    dict_description TEXT,' +
			'    dict_name TEXT,' +
			'    dict_value TEXT,' +
			'    status INTEGER' +
			')';
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('createDictionariesTable success!');
			},
			fail: function(e) {
				console.log('createDictionariesTable failed: ' + JSON.stringify(e));
			}
		});
	},
	createFriendTable: () => {
		db.open();
		let sql = 'create table if not exists t_friend' +
			'(' +
			'    id INTEGER primary key AUTOINCREMENT not null,' +
			'    friend_id INTEGER,' +
			'    create_time NUMERIC,' +
			'    account TEXT,' +
			'    address TEXT,' +
			'    gender INTEGER,' +
			'    head_portrait TEXT,' +
			'    nickname TEXT,' +
			'    phone TEXT,' +
			'    qr_code TEXT,' +
			'    thum_head_portrait TEXT,' +
			'    signature TEXT,' +
			'    status INTEGER,' +
			'    user_id INTEGER,' +
			'    word TEXT' +
			')';
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('createFriendTable success!');
			},
			fail: function(e) {
				console.log('createFriendTable failed: ' + JSON.stringify(e));
			}
		});
	},
	createFriendRequestTable: () => {
		db.open();
		let sql = 'create table if not exists t_friend_request' +
			'(' +
			'    id INTEGER primary key AUTOINCREMENT not null,' +
			'    create_time NUMERIC,' +
			'    modify_time NUMERIC,' +
			'    friend_user_id INTEGER,' +
			'    status INTEGER' +
			')'
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('createFriendRequestTable success!');
			},
			fail: function(e) {
				console.log('createFriendRequestTable failed: ' + JSON.stringify(e));
			}
		});
	},
	excuteSqlTransaction: (sql) => {
		db.open();
		db.transactionDB('begin', () => {
			plus.sqlite.executeSql({
				name: db.name,
				sql: sql,
				success: function(e) {
					console.log('log info ' + app.formatDate(new Date()) + ': ======> ' + sql);
					db.transactionDB('commit');
				},
				fail: function(e) {
					console.log('log error ' + app.formatDate(new Date()) + ' executeSql failed : ======> ' + JSON.stringify(e));
					db.transactionDB('rollback');
				}
			});
		});
	},
	excuteSql: (sql, call1, call2) => {
		db.open();
		plus.sqlite.executeSql({
			name: db.name,
			sql: sql,
			success: function(e) {
				console.log('log info ' + app.formatDate(new Date()) + ': ======> ' + sql);
				if (typeof call1 === 'function') {
					call1();
				}
			},
			fail: function(e) {
				console.log('log error ' + app.formatDate(new Date()) + ': ======> ' + JSON.stringify(e));
				if (typeof call2 === 'function') {
					call2(e);
				}
			}
		});
	},
	/* excuteSqlsTransaction: (sqls) => {
		db.transactionDB('begin', () => {
			for (let sql of sqls) {
				db.excuteSql(sql, )
				plus.sqlite.executeSql({
					name: db.name,
					sql: sql,
					success: function(e) {
						console.log('executeSql success!');
						db.transactionDB('commit');
					},
					fail: function(e) {
						console.log('executeSql failed: ' + JSON.stringify(e));
						db.transactionDB('rollback');
					}
				});
			}
		});
	}, */
	// operation: begin（开始事务）、commit（提交）、rollback（回滚）
	transactionDB: (operation, call) => {
		db.open();
		plus.sqlite.transaction({
			name: db.name,
			operation: operation,
			success: function(e) {
				console.log('transaction success!');
				if (typeof call === 'function') {
					call();
				}
			},
			fail: function(e) {
				console.log('log error ' + app.formatDate(new Date()) + ' transaction failed : ======> ' + JSON.stringify(e));
			}
		});
	},
	// 查询SQL语句
	selectSQL: (sql, call, params) => {
		db.open();
		if (params) {
			let pageNum = params.pageNum;
			let pageSize = params.pageSize;
			if (pageNum && pageSize) {
				sql += ' limit ' + pageSize + ' offset ' + (pageNum - 1) * pageSize;
			}
			if(params.sort){
				sql += params.sort;
			}
		}
		plus.sqlite.selectSql({
			name: db.name,
			sql: sql,
			success: function(data) {
				console.log('log info ' + app.formatDate(new Date()) + ': ======> ' + sql);
				if (typeof call === 'function') {
					call(data);
				}
			},
			fail: function(e) {
				console.log('log error ' + app.formatDate(new Date()) + ': ======> ' + JSON.stringify(e));
			}
		});
	}
}
